//
//  NBHomeViewController.m
//  聚超值
//
//  Created by 李智恒 on 16/3/27.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBHomeViewController.h"
#import "UIImage+Image.h"
#import "NBSelectedController.h"
#import "NBBuyAllVC.h"
#import "NBDiscountVC.h"
#import "NBSeaTradeVC.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height


@interface NBHomeViewController () <UIScrollViewDelegate, SelectedControllerdelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constranit;

// 顶部按钮父控件
@property (weak, nonatomic) IBOutlet UIView *btnView;

// scrollView控件
@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;

@property (nonatomic, weak) UIButton *btn;

@end

@implementation NBHomeViewController

static const CGFloat distance = 104;

-(void)viewDidLoad
{
    [super viewDidLoad];
   
    // 1.0 设置导航条
    [self setNavBar];
    
    // 2.0 初始化scrollerView
    self.scrollerView.contentSize = CGSizeMake(4*ScreenW, ScreenH -distance);
    self.scrollerView.pagingEnabled = YES;
    self.scrollerView.bounces = NO;
    self.scrollerView.delegate = self;
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    self.scrollerView.showsVerticalScrollIndicator = NO;
    
    // 3.0 添加子控制器
    [self setupAllChildViewController];
    
    // 4.0 设置按钮
    [self setBtn];

}

#pragma mark -实现导航条的隐藏
- (void)selected:(NBSelectedController *)selectedController Distance:(CGFloat)offset {
    self.constranit.constant -= offset;
    
    if (self.constranit.constant <= 0) {
        self.constranit.constant = 0;
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        
    } else if (self.constranit.constant >= 64) {
    
        self.constranit.constant = 64;
    }
    
    // 实现导航条特效
    [UIView animateWithDuration:0.01 animations:^{
            self.navigationController.navigationBar.alpha = self.constranit.constant / 64.0;
        
    }];

}

#pragma mark -添加所有子控制器
- (void)setupAllChildViewController
{
    // 精选
    NBSelectedController *selectedVc = [[NBSelectedController alloc] init];
    selectedVc.title = @"精选";
    selectedVc.delegate = self;
    [self addChildViewController:selectedVc];
    
    // 优惠
    NBDiscountVC *discountVc = [[NBDiscountVC alloc] init];
    discountVc.title = @"优惠";
    discountVc.delegate = self;
    [self addChildViewController:discountVc];
    
    // 海淘
    NBSeaTradeVC *seaTradeVc = [[NBSeaTradeVC alloc] init];
    seaTradeVc.title = @"海淘";
    seaTradeVc.delegate = self;
    [self addChildViewController:seaTradeVc];
    
    // 一键购
    NBBuyAllVC *buyAllVc = [[NBBuyAllVC alloc] init];
    buyAllVc.title = @"一键购";
    buyAllVc.delegate = self;
    [self addChildViewController:buyAllVc];
}

#pragma mark -设置按钮
- (void)setBtn {

    NSInteger count = self.childViewControllers.count;
    
    // 按钮初始化设置
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = ScreenW / count;
    CGFloat height = self.btnView.bounds.size.height;
    for (int i = 0; i<count; i++) {
        
        // 获取对应的控制器
        UIViewController *vc = self.childViewControllers[i];
        
        x= width * i;
        // 创建按钮
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(x, y, width, height);
        btn.tag = i;
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        
        [self.btnView addSubview:btn];
        
        // 添加监听方法
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        // 初始化按钮对应的界面
        if (i == 0) {
        
            [self btnClick:btn];
        }
    }
}

#pragma mark -按钮点击事件
- (void)btnClick:(UIButton *)btn {
    
    NSInteger i = btn.tag;
    // 添加控制器的view到scrollView
    UIViewController *vc = self.childViewControllers[i];
    
    vc.view.frame = CGRectMake(btn.tag *ScreenW, 0, ScreenW, self.scrollerView.bounds.size.height);
    
    [self.scrollerView addSubview:vc.view];
    
    // 滚动scrollView
    self.scrollerView.contentOffset = CGPointMake(i*ScreenW, 0);

    // 选中按钮
    [self selectedBtn:btn];
}

// 选中按钮
- (void)selectedBtn:(UIButton *)btn {

    [self.btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.btn = btn;
}
#pragma mark -监听scrollView的滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 发布通知
    [self postNote:scrollView.contentOffset.x noteName:@"stop"];
    
    // 创建界面
    NSInteger i = scrollView.contentOffset.x / ScreenW + 1;
    
    [self setupOneChildViewController:i];

    }

// 减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 发布通知,通知scrollView滚动
    [self postNote:scrollView.contentOffset.x noteName:@"scroll"];

    
    NSInteger i = scrollView.contentOffset.x / ScreenW;
    
    UIButton *btn = self.btnView.subviews[i];
    
    [self selectedBtn:btn];

}
// 停止拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    // 发布通知,通知scrollView滚动
    [self postNote:scrollView.contentOffset.x noteName:@"scroll"];
    
}

#pragma mark -发布通知
- (void)postNote:(CGFloat)offset noteName:(NSString *)name {

    if (offset < ScreenW && offset >= 0) {
        // 发布通知
        [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil];
    }

    
}
#pragma mark -把子控制器的view添加
- (void)setupOneChildViewController:(NSInteger)i
{
    if (i == 4) return;

    UIViewController *vc = self.childViewControllers[i];
    
    // 判断下有没有父控件
    if (vc.view.superview) return;
    
    CGFloat x = i * ScreenW;
    // 设置vc的view的位置
    vc.view.frame = CGRectMake(x, 0, ScreenW, self.scrollerView.bounds.size.height);
    [self.scrollerView addSubview:vc.view];
    
}
#pragma mark -设置导航栏
- (void)setNavBar {

    //设置导航栏左边按钮
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageOriginalWithName:@"pcobb_navbar_btn_search"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBtnClick)];
    
    //设置导航栏右边按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageOriginalWithName:@"pcobb_navbar_btn_uc"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rigthBtnClick)];
    
}
-(void)leftBtnClick
{
    NSLog(@"点击了左边按钮");
}

-(void)rigthBtnClick
{
    NSLog(@"点击了右边按钮");
}
@end
